package com.link.auth.core.interceptors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * HuangLJ
 * 拦截器。当为app登录的时候，用指定的密码校验。
 */
public class MyInterceptor extends HandlerInterceptorAdapter implements HandlerInterceptor {

    private static final Logger logger = LogManager.getLogger(MyInterceptor.class);

    @Resource
    private Environment environment;

    private static ThreadLocal<String> thirdLoginFlag = new ThreadLocal<String>();

    private static ThreadLocal<String> openId = new ThreadLocal<String>();

    private static ThreadLocal<String> thirdType = new ThreadLocal<String>();
    private static ThreadLocal<Long> corpId = new ThreadLocal<>();
    private static ThreadLocal<String> scopeThread = new ThreadLocal<>();

    public static ThreadLocal<String> getScopeThread() {
        return scopeThread;
    }

    public static void setScopeThread(ThreadLocal<String> scopeThread) {
        MyInterceptor.scopeThread = scopeThread;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String thirdName = request.getParameter("thirdName");
        String thirdPasswd = request.getParameter("thirdPasswd");
        String scope = request.getParameter("scope");
        scopeThread.set(scope);
        if (!StringUtils.isEmpty(thirdName) && !StringUtils.isEmpty(thirdPasswd)) {
            //如果传入的的第三方信息不为空，校验是否统一
            String wThirdName = environment.getProperty("appThirdLogin.userName");
            String wThirdPasswd = environment.getProperty("appThirdLogin.passwd");

            if (thirdName.equals(wThirdName) && thirdPasswd.equals(wThirdPasswd)) {
                thirdLoginFlag.set("Y");
            } else {
                thirdLoginFlag.set("N");
            }
        } else {
            thirdLoginFlag.set("N");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    public static ThreadLocal<String> getThirdLoginFlag() {
        return thirdLoginFlag;
    }

    public static void setThirdLoginFlag(ThreadLocal<String> thirdLoginFlag) {
        MyInterceptor.thirdLoginFlag = thirdLoginFlag;
    }

    public static ThreadLocal<String> getOpenId() {
        return openId;
    }

    public static void setOpenId(ThreadLocal<String> openId) {
        MyInterceptor.openId = openId;
    }

    public static ThreadLocal<String> getThirdType() {
        return thirdType;
    }

    public static void setThirdType(ThreadLocal<String> thirdType) {
        MyInterceptor.thirdType = thirdType;
    }

    public static ThreadLocal<Long> getCorpId() {
        return corpId;
    }

    public static void setCorpId(ThreadLocal<Long> corpId) {
        MyInterceptor.corpId = corpId;
    }
}
